package org.diysearch.services;

import org.diysearch.helpers.UserHelper;
import org.diysearch.interfaces.Product;
import org.diysearch.models.Status;
import org.diysearch.models.User;
import org.diysearch.products.JsonProduct;

public class ValidateUsername extends BaseService {
	private String uri;
	
	@Override
	public Product execute() throws Exception {
		Product p = new JsonProduct();
		Status s = new Status();
		String username = request.getParameter("username");
		User u = new User();
		u.setUsername(username);
		User user = UserHelper.getUser(u, new String[] {});
		
		if (user == null) {
			// username does NOT exist, we are good to go
			s.setCode(1);
			s.setMessage("ok");
		} else {
			// username exists
			s.setCode(-1);
			s.setMessage("user already exists");
		}
		p.setWriter(writer);
		p.setOutput(s);
		return p;
	}

	@Override
	public boolean isWriter() {
		return true;
	}

	@Override
	public String getUri() {
		return uri;
	}

	@Override
	public void setUri(String uri) {
		this.uri=uri;
	}

}
